﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LsMisCommon;

namespace card
{
    public partial class SRModifyForm : Form
    {
        public static WebDG.Service ws = new card.WebDG.Service();

        public string shipper
        {
            get
            {
                string _shipper = "";
                if (this.comboBox_owner.SelectedValue != null)
                {
                    _shipper = this.comboBox_owner.SelectedValue.ToString().Trim();
                }
                else
                {
                    _shipper = this.comboBox_owner.Text.ToString().Trim();
                }
                return _shipper;
            }
        }
        public string stackname
        {
            get
            {
                string _stackname = "";
                if (this.comboBox_stackName.SelectedValue != null)
                    _stackname = this.comboBox_stackName.SelectedValue.ToString();
                else
                    _stackname = this.comboBox_stackName.Text.Trim();
                return _stackname;
            }
        }
        public string billno
        {
            get
            {
                return this.textBox_billNO.Text.ToString();
            }
        }
        public string material
        {
            get
            {
                return this.textBox_material.Text.ToString().ToUpper();
            }
        }
        public string specification
        {
            get
            {
                return this.textBox_specification.Text.ToString();
            }
        }
        public string pieces
        {
            get
            {
                string _pieces = this.textBox_pieces.Text.ToString().Trim();
                if (_pieces.Length == 0) _pieces = "0";
                return _pieces;
            }
        }
        public string tons
        {
            get
            {
                string _tons = this.textBox_tons.Text.ToString().Trim();
                if (_tons.Length == 0) _tons = "0";
                return _tons;
            }
        }
        public string squares
        {
            get
            {
                string _squares = this.textBox_squares.Text.ToString().Trim();
                if (_squares.Length == 0) _squares = "0";
                return _squares;
            }
        }
        public string yard
        {
            get
            {
                return this.textBox_yard.Text.ToString().Trim();
            }
        }
        public string getowner
        {
            get
            {
                string _getOwner = "";
                if (this.comboBox_owner.SelectedValue != null)
                {
                    _getOwner = this.ComboBox_getOwner.SelectedValue.ToString().Trim();
                }
                else
                {
                    _getOwner = this.ComboBox_getOwner.Text.ToString().Trim();
                }
                return _getOwner;
            }
        }
        public string comment
        {
            get
            {
                return this.textBox_comment.Text.ToString();
            }
        }

        public SRModifyForm()
        {
            InitializeComponent();
            Init();
        }

        public SRModifyForm(string shipper, string stackname, string billno, string material, string specification, string pieces, string tons, string squares, string yard, string getowner, string comment)
            : this()
        {
            SetDefaultValue(shipper, stackname, billno, material, specification, pieces, tons, squares, yard, getowner, comment);
        }

        private void Init()
        {
            this.comboBox_owner.DataSource = Session.Shippers.Tables[0];
            this.comboBox_owner.ValueMember = "SHIPPER_NAME";
            this.comboBox_owner.DisplayMember = "DESCRIPTION";

            string sql_shippers = "select SHIPPER_NAME,SHIPPER_BRIEF,SHIPPER_BRIEF||'/'||SHIPPER_SHOT as DESCRIPTION from V_SHIPPER_DOC_TCC";
            DataSet ds_shippers = ws.getDataSet_Serv(sql_shippers);
            this.ComboBox_getOwner.DataSource = ds_shippers.Tables[0];
            this.ComboBox_getOwner.ValueMember = "SHIPPER_NAME";
            this.ComboBox_getOwner.DisplayMember = "DESCRIPTION";

            this.comboBox_stackName.DataSource = Session.Cargo_names.Tables[0];
            this.comboBox_stackName.ValueMember = "CARGO_NAM";
            this.comboBox_stackName.DisplayMember = "DISPLAYNAME";

            this.comboBox_owner.Text = "";
            this.ComboBox_getOwner.Text = "";
        }

        private void SetDefaultValue(string shipper, string stackname, string billno, string material, string specification, string pieces, string tons, string squares, string yard, string getowner, string comment)
        {
            SetComboBoxValue(comboBox_owner, shipper);
            //this.comboBox_owner.SelectedValue = shipper;
            //if (this.comboBox_owner.SelectedValue == null)
            //    this.comboBox_owner.Text = shipper;
            SetComboBoxValue(comboBox_stackName, stackname);
            //this.comboBox_stackName.SelectedValue = stackname;
            //if (this.comboBox_stackName.SelectedValue == null)
            //    this.comboBox_stackName.Text = stackname;
            this.textBox_billNO.Text = billno;
            this.textBox_material.Text = material;
            this.textBox_specification.Text = specification;
            this.textBox_pieces.Text = pieces;
            this.textBox_tons.Text = tons;
            this.textBox_squares.Text = squares;
            this.textBox_yard.Text = yard;
            SetComboBoxValue(ComboBox_getOwner, getowner);
            //this.ComboBox_getOwner.SelectedValue = getowner;
            //if (this.ComboBox_getOwner.SelectedValue == null)
            //    this.ComboBox_getOwner.Text = getowner;
            this.textBox_comment.Text = comment;
        }

        private void SetComboBoxValue(UCComboBox com, string value)
        {
            com.SelectedText = "";//先置空
            com.SelectedValue = value;
            if (com.SelectedValue == null)
                com.Text = value;
        }

        private bool check_input()
        {
            string shipper = "";
            if (this.comboBox_owner.SelectedValue != null)
            {
                shipper = this.comboBox_owner.SelectedValue.ToString().Trim();
            }
            else
            {
                shipper = this.comboBox_owner.Text.ToString().Trim();
            }
            string stack_name = "";
            if (this.comboBox_stackName.SelectedValue != null)
                stack_name = this.comboBox_stackName.SelectedValue.ToString().Trim();
            else
                stack_name = this.comboBox_stackName.Text.Trim();
            string pieces = this.textBox_pieces.Text.ToString().Trim();
            string tons = this.textBox_tons.Text.ToString().Trim();

            if (shipper.Length == 0)
            {
                MessageBox.Show("货主/代理不允许为空!");
                return false;
            }
            if (stack_name.Length == 0)
            {
                MessageBox.Show("货名不允许为空!");
                return false;
            }
            if (pieces.Length == 0)
            {
                MessageBox.Show("件数不允许为空!");
                this.textBox_pieces.Focus();
                return false;
            }
            if (tons.Length == 0)
            {
                MessageBox.Show("吨数不允许为空!");
                this.textBox_tons.Focus();
                return false;
            }
            return true;
        }

        private void SRModifyForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.NumPad1 && e.Control)
            {
                this.button_gg.PerformClick(); //perform the click action of btn_car      
            }
            if (e.KeyCode == Keys.D1 && e.Control)
            {
                this.button_gg.PerformClick(); //perform the click action of btn_car      
            }
            if (e.KeyCode == Keys.F2)
            {
                this.button_gg.PerformClick();
            }
        }

        #region the key press event handler for replacing the tab with Enter key

        private void comboBox_owner_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)13)
            {
                return;
            }
            else
            {
                this.comboBox_stackName.Focus();
            }
        }

        private void comboBox_stackName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)13)
            {
                return;
            }
            else
            {
                this.textBox_billNO.Focus();
            }
        }

        private void textBox_billNO_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)13)
            {
                return;
            }
            else
            {
                this.textBox_material.Focus();
            }
        }

        private void textBox_material_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_specification.Focus();
            }
        }

        private void textBox_specification_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_pieces.Focus();
            }
        }

        private void textBox_pieces_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_tons.Focus();
            }
        }

        private void textBox_tons_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_squares.Focus();
            }
        }

        private void textBox_squares_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                //this.textBox_comment.Focus();
                this.textBox_yard.Focus();
            }
        }

        private void textBox_yard_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                //this.textBox_comment.Focus();
                this.ComboBox_getOwner.Focus();
            }
        }

        private void comboBox_getOwner_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_comment.Focus();
            }
        }

        #endregion

        #region the specification event handlers

        private void button_gg_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("ssssss");
            string stack_name = this.comboBox_stackName.Text.ToString();
            int index = stack_name.IndexOf("钢板");
            if (index != -1)
            {
                this.groupBox_gbgg.Left = this.textBox_specification.Left;
                this.groupBox_gbgg.Top = this.button_gg.Top + 200;
                this.groupBox_gbgg.Visible = true;
                this.textBox_gb_hd.Focus();

                //Hide all other specification input window
                this.groupBox_pygg.Visible = false;
                this.groupBox_yg_gg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;
            }
            if (stack_name.IndexOf("盘元") != -1)
            {
                this.groupBox_pygg.Left = this.textBox_specification.Left;
                this.groupBox_pygg.Top = this.button_gg.Top + 200;
                this.groupBox_pygg.Visible = true;
                this.textBox_py_zj.Focus();

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_yg_gg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;

            }
            if (stack_name.IndexOf("圆钢") != -1)
            {
                this.groupBox_yg_gg.Left = this.textBox_specification.Left;
                this.groupBox_yg_gg.Top = this.button_gg.Top + 200;
                this.groupBox_yg_gg.Visible = true;
                this.textBox_yg_zj.Focus();
                this.groupBox_yg_gg.Text = "圆钢规格";

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_pygg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;
            }
            if (stack_name.IndexOf("螺纹钢") != -1)
            {
                this.groupBox_yg_gg.Left = this.textBox_specification.Left;
                this.groupBox_yg_gg.Top = this.button_gg.Top + 200;
                this.groupBox_yg_gg.Visible = true;
                this.textBox_yg_zj.Focus();
                this.groupBox_yg_gg.Text = "螺纹钢规格";

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_pygg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;
            }
            if (stack_name.IndexOf("卷钢") != -1)
            {
                this.groupBox_jg_gg.Left = this.textBox_specification.Left;
                this.groupBox_jg_gg.Top = this.button_gg.Top + 200;
                this.groupBox_jg_gg.Visible = true;
                this.textBox_jg_hd.Focus();

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_pygg.Visible = false;
                this.groupBox_yg_gg.Visible = false;
                this.groupBox_Hg_gg.Visible = false;
            }
            if (stack_name.IndexOf("H钢") != -1)
            {
                this.groupBox_Hg_gg.Left = this.textBox_specification.Left;
                this.groupBox_Hg_gg.Top = this.button_gg.Top + 200;
                this.groupBox_Hg_gg.Visible = true;
                this.textBox_Hg_gd.Focus();

                //Hide all other specification input window
                this.groupBox_gbgg.Visible = false;
                this.groupBox_pygg.Visible = false;
                this.groupBox_yg_gg.Visible = false;
                this.groupBox_jg_gg.Visible = false;
            }
        }

        private void button_gg_gb_Click(object sender, EventArgs e)
        {
            string gb_hd = this.textBox_gb_hd.Text.ToString().Trim();
            string gb_kd = this.textBox_gb_kd.Text.ToString().Trim();
            string gb_cd = this.textBox_gb_cd.Text.ToString().Trim();
            if (gb_hd.Length == 0 || gb_kd.Length == 0 || gb_cd.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = gb_hd + "mm × " + gb_kd + "mm × " + gb_cd + "mm";
            }
            this.groupBox_gbgg.Visible = false;
            this.textBox_pieces.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string py_zj = this.textBox_py_zj.Text.ToString().Trim();
            if (py_zj.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = "Ф" + py_zj + "mm";
            }
            this.groupBox_pygg.Visible = false;
            this.textBox_pieces.Focus();
        }

        private void button_yg_gg_Click(object sender, EventArgs e)
        {
            string yg_zj = this.textBox_yg_zj.Text.ToString().Trim();
            string yg_cd = this.textBox_yg_cd.Text.ToString().Trim();
            if (yg_zj.Length == 0 || yg_cd.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = "Ф" + yg_zj + "mm × " + yg_cd + "m";
            }
            this.groupBox_yg_gg.Visible = false;
            this.textBox_pieces.Focus();
        }

        private void button_jg_Click(object sender, EventArgs e)
        {
            string jg_hd = this.textBox_jg_hd.Text.ToString().Trim();
            string jg_kd = this.textBox_jg_kd.Text.ToString().Trim();
            if (jg_hd.Length == 0 || jg_hd.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = jg_hd + "mm × " + jg_kd + "mm";
            }
            this.groupBox_jg_gg.Visible = false;
            this.textBox_pieces.Focus();
        }

        private void button_Hg_Click(object sender, EventArgs e)
        {
            string Hg_gd = this.textBox_Hg_gd.Text.ToString().Trim();
            string Hg_kd = this.textBox_Hg_kd.Text.ToString().Trim();
            string Hg_cd = this.textBox_Hg_cd.Text.ToString().Trim();
            if (Hg_gd.Length == 0 || Hg_gd.Length == 0 || Hg_gd.Length == 0)
            {
                this.textBox_specification.Text = "";
            }
            else
            {
                this.textBox_specification.Text = Hg_gd + "mm × " + Hg_kd + "mm × " + Hg_cd + "mm";
            }
            this.groupBox_Hg_gg.Visible = false;
            this.textBox_pieces.Focus();
        }

        #endregion

        #region=====================钢板规格回车==========================
        private void textBox_gb_hd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_gb_kd.Focus();
            }
        }

        private void textBox_gb_kd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_gb_cd.Focus();
            }
        }

        private void textBox_gb_cd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_gg_gb.Focus();
            }
        }

        private void button_gg_gb_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_gg_gb.PerformClick();
                //this.textBox_pieces.Focus();
            }
        }
        #endregion
        #region==============盘元规格回车==========================
        private void textBox_py_zj_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button1.Focus();
            }
        }

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button1.PerformClick();
            }
        }
        #endregion
        #region================圆钢规格回车========================
        private void textBox_yg_zj_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_yg_cd.Focus();
            }
        }

        private void textBox_yg_cd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_yg_gg.Focus();
            }
        }
        private void button_yg_gg_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_yg_gg.PerformClick();
            }
        }
        #endregion
        #region=====================卷钢规格回车==================
        private void textBox_jg_hd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_jg_kd.Focus();
            }
        }

        private void textBox_jg_kd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_jg.Focus();
            }
        }

        private void button_jg_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_jg.PerformClick();
            }
        }
        #endregion
        #region===================H钢规格回车=========================
        private void textBox_Hg_gd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_Hg_kd.Focus();
            }
        }

        private void textBox_Hg_kd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_Hg_cd.Focus();
            }
        }

        private void textBox_Hg_cd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_Hg.Focus();
            }
        }

        private void button_Hg_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_Hg.PerformClick();
            }
        }
        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (check_input())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
